package cc.lzmsh.service.impl;

import cc.lzmsh.constants.SystemConstants;
import cc.lzmsh.domain.entity.Comment;
import cc.lzmsh.domain.result.Result;
import cc.lzmsh.domain.vo.CommentVo;
import cc.lzmsh.domain.vo.PageVo;
import cc.lzmsh.enums.AppHttpCodeEnum;
import cc.lzmsh.exception.SystemException;
import cc.lzmsh.mapper.CommentMapper;
import cc.lzmsh.service.CommentService;
import cc.lzmsh.service.UserService;
import cc.lzmsh.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author msh
 * @since 2024-09-19 20:20:00
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private UserService userService;


    @Override
    public Result<PageVo<List<CommentVo>>> CommentList(String contentType, Long articleId, Integer pageNum, Integer pageSize) {
        // 查询评论列表
        // 1.查询根评论评论
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        // 判断是不是文章评论
        wrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(contentType), "article_id", articleId);
        // 根评论
        wrapper.eq("root_id", -1);
        // 评论排序
        wrapper.orderByAsc("create_time");
        // 评论类型
        wrapper.eq("type", contentType);

        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<CommentVo> commentVos = toCommentVoList(page.getRecords());

        // 2.查询子评论，并存入commentVo中
        commentVos = commentVos.stream().peek(commentVo -> {
            commentVo.setChildren(getChildren(commentVo.getId()));
        }).collect(Collectors.toList());
        PageVo<List<CommentVo>> pageVo = new PageVo(page.getTotal(), commentVos);
        return Result.success(pageVo);
    }

    @Override
    public Result addComment(Comment comment) {
        // 评论内容不能为空
        if (!StringUtils.hasText(comment.getContent())) {
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return Result.success();
    }

    private List<CommentVo> getChildren(Long id) {

        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        // 查询子评论
        queryWrapper.eq("to_comment_id", id);
        queryWrapper.orderByAsc("create_time");
        List<Comment> comments = list(queryWrapper);
        return toCommentVoList(comments);
    }

    public List<CommentVo> toCommentVoList(List<Comment> commentList) {
        // 给赋值评论者名和评论者回复者名
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(commentList, CommentVo.class);
        return commentVos.stream().peek(commentVo -> {
            commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getNickName());
            if (commentVo.getToCommentUserId() != -1) {
                commentVo.setToCommentUserName(userService.getById(commentVo.getToCommentUserId()).getNickName());
            }
        }).collect(Collectors.toList());
    }
}

